!
!   ROBUSTMEAN -  Library functions to robust estimation of the mean.
!
     
!-----------------------------------------------------------------------------

       Subroutine RobustMean(array, n, mean, sig )

!       use MQuick
!       use MMModule
    
       use qmedian
       use cutfun
       use RobMean
       
       Integer :: n,i
       Real :: array(n), mean, sig

! Input: 
!      array ... data
!
! Output:
!      n     ... No. of data used to estimation (after clip)
!      mean ... resulting mean
!      sig  ... estimation of the standart deviation
!
       Real :: median, mode, skew

!       write(*,*)  '>',n
       call am1(n,array,mean,sig,hampel,dhampel,50,i)
       sig = sig*sqrt(1.0*n)
       if( i == 1 ) then
          mean = 0.0
          sig = -1.0
       endif
!       write(*,*) "status=",i,"mean,sig,no=",mean,sig,n

! This is Stetson's original estimator for mean
!       n = size(array)
!       Call QuickSort(array)
!
!       if( n > MINARRAY ) then    ! normal case
!
!          Call MMM(n, array, 1e10, mean, median, mode, sig, skew)  
!
!       else                       ! pathological case - a few data points
!                
!          mean = 0.5*(array((n + 1)/2) + array(n/2 + 1))    
!          sig = 0.0
!  
!       endif          
!       write(*,*) "std: mean,sig=",mean,sig

       End
